package com.cdx.jvm;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author chedx
 * @data 2021/1/25 10:50 PM
 */
public class JVMTestMain76 {
    public static void main(String[] args) {
        long count = 0;
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Car.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    System.out.println("汽车启动前，检查安全。");
                    return methodProxy.invokeSuper(o,objects);
                }
            });
            Car car = (Car) enhancer.create();
            car.run();
            System.out.println("while 循环进行了:"+ (++count) + "次");
        }

    }

     static class Car{
        public void run () {
            System.out.println("汽车启动，点火。。。。");
        }
    }
}
